package com.zhuoyue.common.config;

import com.fasterxml.classmate.TypeResolver;
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.List;

@Configuration
@EnableSwagger2
@EnableKnife4j
@Import(BeanValidatorPluginsConfiguration.class)
public class SwaggerConfiguration {

    private final TypeResolver typeResolver;
    /**
     * 设置请求的统一前缀
     */
    @Value("${swagger.pathMapping}")
    private String pathMapping;

    @Autowired
    public SwaggerConfiguration(TypeResolver typeResolver) {
        this.typeResolver = typeResolver;
    }

    /*@Bean
    public UiConfiguration uiConfiguration(){
        return UiConfigurationBuilder.builder().supportedSubmitMethods(new String[]{})
                .displayOperationId(true)
                .build();
    }*/


    @Bean(value = "defaultApi")
    public Docket defaultApi() {
//        ParameterBuilder parameterBuilder=new ParameterBuilder();
//        List<Parameter> parameters= Lists.newArrayList();
//        parameterBuilder.name("organizationCode").description("组织编码").modelRef(new ModelRef("String"))
//                .parameterType("header")
//                .required(true).build();
//        parameters.add(parameterBuilder.build());

        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("卓悦智能后台管理系统接口文档")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.zhuoyue.web.controller"))
                //.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build()
                .pathMapping(pathMapping)
//                .globalOperationParameters(parameters)
                .securityContexts(Lists.newArrayList(securityContext())).securitySchemes(Lists.<SecurityScheme>newArrayList(apiKey(), apiKey1()));
        return docket;
    }

//    @Bean(value = "groupRestApi")
//    public Docket groupRestApi() {
//        List<ResolvedType> list=Lists.newArrayList();
//
//        //SpringAddtionalModel springAddtionalModel= springAddtionalModelService.scan("com.swagger.bootstrap.ui.demo.extend");
//        return new Docket(DocumentationType.SWAGGER_2)
//                .apiInfo(groupApiInfo())
//                .groupName("分组接口")
//                .select()
//                .apis(RequestHandlerSelectors.basePackage("com.swagger.bootstrap.ui.demo.group"))
//                .paths(PathSelectors.any())
//                .build()
//                .additionalModels(typeResolver.resolve(DeveloperApiInfo.class)).securityContexts(Lists.newArrayList(securityContext(),securityContext1())).securitySchemes(Lists.<SecurityScheme>newArrayList(apiKey(),apiKey1()));
//    }

//    private ApiInfo groupApiInfo(){
//        DeveloperApiInfoExtension apiInfoExtension=new DeveloperApiInfoExtension();
//
//        apiInfoExtension.addDeveloper(new DeveloperApiInfo("张三","zhangsan@163.com","Java"))
//        .addDeveloper(new DeveloperApiInfo("李四","lisi@163.com","Java"));
//
//
//        return new ApiInfoBuilder()
//                .title("卓悦智能APP接口文档")
//                .description("<div style='font-size:14px;color:red;'>目前处于测试阶段</div>")
//                .termsOfServiceUrl("http://www.group.com/")
//                .contact("wjming")
//                .version("1.0")
//                .extensions(Lists.newArrayList(apiInfoExtension))
//                .build();
//    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("卓悦智能后台管理系统接口文档")
                .description("卓悦智能后台管理系统后端所有接口")
                .termsOfServiceUrl("http://www.xx.com/")
                .contact("hewenze")
                .version("1.0")
                .build();
    }

    private ApiKey apiKey() {
        return new ApiKey("TOKEN", "Authorization", "header");
    }

    private ApiKey apiKey1() {
        return new ApiKey("组织编码", "organizationCode", "header");
    }

    private SecurityContext securityContext() {
        return SecurityContext.builder()
                .securityReferences(defaultAuth())
                .forPaths(PathSelectors.regex("/.*"))
                .build();
    }
//    private SecurityContext securityContext1() {
//        return SecurityContext.builder()
//                .securityReferences(defaultAuth1())
//                .forPaths(PathSelectors.regex("/.*"))
//                .build();
//    }

    List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        return Lists.newArrayList(new SecurityReference("BearerToken", authorizationScopes));
    }
//    List<SecurityReference> defaultAuth1() {
//        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
//        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
//        authorizationScopes[0] = authorizationScope;
//        return Lists.newArrayList(new SecurityReference("BearerToken1", authorizationScopes));
//    }

}
